#!/bin/sh
#desc: Generate configuration file for local services
#package:oddiscovery
#type:reserved

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later

TMPFILE="/tmp/$RANDOM-services"
rm -f $TMPFILE
SVCDIR="/usr/local/opendomo/services"

echo "#Services exported:"
echo -e "#service\tname\tuser\tgroup\tpermissions\ttype\tclass\thost"

# Directories
for dir in `find $SVCDIR/ -type d | cut -d '/' -f6-`
do

   # Scripts
   for file in `find $SVCDIR/$dir/ -name '*.sh' -maxdepth 1`
   do
      ls -la $file | cut -b0-11,17-27,26-34,57- |  sed 's/ \+/ /g'>> $TMPFILE
   done

   # Configuration
   for file in `find $SVCDIR/$dir/ -name '*.sh' -maxdepth 1`
   do
      SVCNAME=`basename $file`
      if test "$SVCNAME" != "`basename $0`"; then
         SVCPERMS=`grep $SVCNAME $TMPFILE | cut -f1 -d' '`
         SVCUSR=`grep $SVCNAME $TMPFILE | cut -f2 -d' ' `
         SVCGRP=`grep $SVCNAME $TMPFILE | cut -f3 -d' '`
         SVCTYPE=`grep "\#type:" $file | sed 's/#type://' `
         SVCCLASS=$dir
         if test "$SVCTYPE" == ""; then
            SVCTYPE="local"
      fi
      echo -e "service \t$SVCNAME\t$SVCUSR\t$SVCGRP\t$SVCPERMS\t$SVCTYPE\t$SVCCLASS\tlocalhost\t"
   fi
   done

   rm -f $TMPFILE

done



